package net.maku.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import net.maku.bean.model.LoginFailException;
import net.maku.bean.model.MyException;
import net.maku.bean.model.RespStatus;
import net.maku.utils.ErrorCode;
import net.maku.utils.Result;

@Slf4j
@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GExceptionController {
	
	//正常返回，提示错误信息
	@ResponseBody
	@ExceptionHandler(value = LoginFailException.class)
	public Result<String> loginFailHandler( HttpServletRequest req, LoginFailException ex )  {
		log.info( "LoginFailException: " + ex.getMessage());
		return Result.error(ErrorCode.REFRESH_TOKEN_INVALID.getCode(), "登录失败");
	}

	//正常返回，提示错误信息
	@ResponseBody
	@ExceptionHandler(value = MyException.class)
	public Result<String> myErrorHandler( HttpServletRequest req, MyException ex )  {
		//	ex.printStackTrace();
		log.info( "MyErrorHandler: " + ex.getMessage());
		//return ReturnData.failed( "MY-FAIL-1 :"+ex.getMessage() );
		return Result.error("后台业务异常:"+ex.getMessage());
	}
	
	//正常返回，提示错误信息
	@ResponseBody
	@ExceptionHandler(value = MethodArgumentNotValidException.class)
	public Result<String> myErrorHandler( HttpServletRequest req, MethodArgumentNotValidException ex )  {
		String msg="";
		BindingResult bindingResult = ex.getBindingResult();
	    for (FieldError fieldError : bindingResult.getFieldErrors()) {
	            //String fieldName = fieldError.getField();
	            //String errorMessage = fieldError.getDefaultMessage();
	            // 在这里可以对字段名和错误信息进行处理
	            //System.out.println("字段名：" + fieldName);
	            //System.out.println("错误信息：" + errorMessage);
	        	msg = fieldError.getDefaultMessage();
	    }
	    log.info( "MyErrorHandler: " + ex.getMessage());
	    return Result.error( "参数校验异常:"+msg );
	}

	@ResponseBody
	@ExceptionHandler(value = Throwable.class)
	public ResponseEntity<String> defaultErrorHandler( HttpServletRequest req, Throwable ex ) throws Exception {
			ex.printStackTrace();
			System.out.println( "=======================================");

			System.out.println( ex.getClass().getName() );
			log.info( "defaultErrorHandler: " + ex.toString() );
			String msg = expMap.get( ex.getClass().getName() );
			if( msg !=null ) {
				return RespStatus.SUCCESS.getRespone( Result.error( msg ) );
			}
			return RespStatus.FAIL.getRespone( "后台异常:"+ex.getMessage() );
	}
	
	//处理异常类型
	Map<String, String > expMap = new HashMap<String, String>(){
	    {
	        put( "org.springframework.security.access.AccessDeniedException", "用户没有访问权限" );
	        put( "java.lang.NullPointerException", "访问空数据异常" ); 
	    }
	};

		
}
